package com.example.homework.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import com.bumptech.glide.Glide
import com.example.homework.databinding.PhotoRecyclerViewItemBinding
import com.example.homework.models.Photo

class PhotoPagedAdapter(val onClick: (View, String) -> Unit) :
    PagingDataAdapter<Photo, PhotosRecyclerViewHolder>(DiffUtilCallback()) {
    override fun onBindViewHolder(holder: PhotosRecyclerViewHolder, position: Int) {
        val item: Photo? = getItem(position)
        item?.let {
            with(holder.binding) {
                cameraTextView.text = item.camera.name
                dateTextView.text = item.earthDate
                solTextView.text = item.sol
                roverTextView.text = item.rover.name
                Glide.with(photoImageView.context).load(item.imgSrc)
                    .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                    .error(android.R.drawable.stat_notify_error).into(photoImageView)
                val transitionName = "photo_${item.id}"
                photoImageView.transitionName = transitionName
                root.setOnClickListener {
                    onClick(photoImageView, item.imgSrc)
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotosRecyclerViewHolder {
        return PhotosRecyclerViewHolder(
            PhotoRecyclerViewItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }
}